<template>
    <div id="login">
        <div id="loginPart">
            <div id="title">登录</div>
            <div id="loginUsr">
                <i class="el-icon-s-custom"></i>
                <input type="text" v-model="username" placeholder="用户名">
            </div>
            <div id="loginPwd">
                <i class="el-icon-lock"></i>
                <input type="password" v-model="password" placeholder="密码" @keyup.enter="login">
            </div>
            <div id="loginBtn">
                <i class="el-button" @click="login">登录</i>
            </div>
            <div id="other">
                <p id="forget" @click="forgetPassword">忘记密码？</p>
                <router-link to="/register">还没有账户？立即注册</router-link>
            </div>
            
        </div>
    </div>
</template>

<script>

import {loginSubmit} from '../network/product'

export default {
    name: 'login',
    data() {
        return {
            username: '',
            password: ''
        }
    },
    methods: {
        forgetPassword() {
            this.$message({
                message: "请联系管理员修改密码，学号19373367",
                type: "success",
            });
        },
        login(){
            console.log(this.username);
            console.log(this.password);
            if (!this.username || !this.password) {
                this.$message({
                message: "用户名或密码不能为空",
                type: "warning"
                });
                return;
            }
            loginSubmit(this.username, this.password)
                .then(res => {
                    console.log(res);
                    if (res.data.res == 1) {
                        this.$message.error("用户名或密码错误");
                        return;
                    }  else {
                        this.$message({
                        message: "恭喜你，登录成功！",
                        type: "success"
                        });
                        this.$store.commit("login");
                        this.$store.commit("setUsername", this.username)
                    }
                })
                .catch(err => {
                this.$message.error("注册失败了~请检查网络并再次尝试");
                return;
                });
                this.$router.push('/')
        }
    }
}

</script>

<style scoped>

#login{
    background-color: white;
    padding: 20px;
    background-image: url('../assets/img/loginBG.jpg');
    background-size: 100%;
    background-position: 0px -175px;
}

#loginPart {
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    height: 400px;
    width: 300px;
    font-size: 20px;
    margin-left: 750px;
    border: 1px solid black;
    background-color: rgba(244, 244, 244, 0.2);
}

#loginUsr input, #loginPwd input{
    line-height: 26px;
    width: 260px;
    outline: none;
    padding-left: 5px;
    font-size: 20px;
}

.el-button {
    font-style: normal;
    width: 200px;
    font-size: 20px;
}

#other p {
    cursor: pointer;
}


</style>